<?php

namespace app\admin\service;


use app\admin\model\SystemLog;
use app\admin\model\SystemNode;

/**
 * 系统日志表
 * Class SystemLogService
 * @package app\admin\service
 */
class SystemLogService
{

    /**
     * 当前实例
     * @var object
     */
    protected static $instance;

    /**
     * 记录的接口列表
     * @var string
     */
    protected $urlList = [
        /*system*/
        '/admin/system/adminEdit',
        '/admin/system/adminDel',
        '/admin/system/adminEditStatus',
        '/admin/system/authEdit',
        '/admin/system/authDel',
        '/admin/system/setAuthStatus',
        '/admin/system/authNode',
        '/admin/system/refreshNode',
        '/admin/system/editPassword',
    ];

    /**
     * 构造方法
     * SystemLogService constructor.
     */
    protected function __construct()
    {
        return $this;
    }

    /**
     * 获取实例对象
     * @return SystemLogService|object
     */
    public static function instance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new static();
        }
        return self::$instance;
    }


    /**
     * 保存数据
     * @param $data
     * @return bool|string
     */
    public function save($data)
    {
        if (!in_array($data['url'], $this->urlList)) {
            return false;
        }
        $systemNode = new SystemNode();
        $data['title'] = (string)$systemNode->where('node', str_replace('/admin/', '', $data['url']))->value('title');
        (new SystemLog())->insert($data);
        return true;
    }
}
